package com.yht.springboot_yht.service;

import com.yht.springboot_yht.pojo.User;
import com.yht.springboot_yht.pojo.dto.UserDto;
import com.yht.springboot_yht.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service   //sprint bean
public class UserService implements IUserService {

    @Autowired
    UserRepository userRepository;


    @Override
    public User add(UserDto user) {
        User userPojo = new User();
        BeanUtils.copyProperties(user,userPojo);
        return userRepository.save(userPojo);
    }

    @Override
    public User getUser(Integer userId) {
        return userRepository.findById(userId).orElseThrow(()->{throw new IllegalArgumentException("用户不存在，存在异常！");});
    }

    @Override
    public User edit(UserDto user) {
        User userPojo = new User();
        BeanUtils.copyProperties(user,userPojo);
        return userRepository.save(userPojo);
    }

    @Override
    public void delete(Integer userId) {
         userRepository.deleteById(userId);
    }
}
